import requests
import re  # 正则

from bs4 import BeautifulSoup
import os

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

a = [{"domain": ".chineselaw.com", "expirationDate": "1713454759.656437", "hostOnly": "false", "httpOnly": "false",
      "name": "gr_user_id", "path": "/", "sameSite": "unspecified", "secure": "false", "session": "false",
      "storeId": "0",
      "value": "35cfe448-e3bb-4cc9-936f-6a4f1580d9ef"},
     {"domain": ".chineselaw.com", "expirationDate": "1710430759", "hostOnly": "false", "httpOnly": "false",
      "name": "Hm_lvt_762a6cd324472973aa9934517a8ecb5a", "path": "/", "sameSite": "unspecified", "secure": "false",
      "session": "false", "storeId": "0", "value": "1678885865,1678892293"},
     {"domain": "ydzk.chineselaw.com", "hostOnly": "true", "httpOnly": "false", "name": "ZKJS_SESSION_ID", "path": "/",
      "sameSite": "unspecified", "secure": "false", "session": "true", "storeId": "0",
      "value": "8246510b-9c00-4c7c-b848-0d6d9b13cc75"},
     {"domain": ".chineselaw.com", "hostOnly": "false", "httpOnly": "true", "name": "token", "path": "/",
      "sameSite": "no_restriction", "secure": "true", "session": "true", "storeId": "0",
      "value": "1a18a056cb114b8f91bd2568d6d963a7"},
     {"domain": ".chineselaw.com", "hostOnly": "false", "httpOnly": "true", "name": "ZKCOOKIE", "path": "/",
      "sameSite": "no_restriction", "secure": "true", "session": "true", "storeId": "0",
      "value": "4344bc66-f3d7-44da-b9c5-18079987eb06"},
     {"domain": ".chineselaw.com", "hostOnly": "false", "httpOnly": "false",
      "name": "Hm_lpvt_762a6cd324472973aa9934517a8ecb5a", "path": "/", "sameSite": "unspecified", "secure": "false",
      "session": "true", "storeId": "0", "value": "1678894760"}]

htmls = [
    "https://szlx.pkulaw.com/law",
    "https://alphalawyer.cn/home",
    "https://www.faxin.cn",
    "https://law.wkinfo.com.cn/",
    "https://www.itslaw.com/home",
    "https://www.chineselaw.com/"
]

data1 = {"source": "PTAL",
        "id": "cbb60fc711a62b5f2d0c58269ca0dda3",
        "title": "黄春明与何青海、四川民兴旺业房地产有限公司建设工程施工合同纠纷一审民事判决书",
        "browserType": "chrome",
        "isSaveBj": "true"}

data = {"source": "PTAL","id": "cbb60fc711a62b5f2d0c58269ca0dda3"}

html = "https://ydzk.chineselaw.com/ydzk/api/writDownload"

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/52.0.2743.116 Safari/537.36 '
}


def create_dir_not_exist(path):
    if not os.path.exists(path):
        os.mkdir(path)

def getCookies(cookieList):
    cookies_dict = {}
    for i in cookieList:
        cookies_dict[i["name"]] = i["value"]
    return cookies_dict


s = Service("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
if __name__ == "__main__":
    driver = webdriver.Chrome(service=s)
    url = "https://ydzk.chineselaw.com"
    driver.get(url)
    # 获取cookie列表
    cookie_list = driver.get_cookies()
    # 格式化打印cookie
    print(cookie_list)
    cookie = getCookies(a)
    res = requests.post(url=html, data=data, headers=headers, cookies=cookie).content

    create_dir_not_exist("././files")
    with open("././files/%s.docx" % "ccc", "wb") as f:
        f.write(res)
    print(res)
